{% extends 'rebs/layouts/base.html' %}

{% block title %}Rebs - Project{% endblock %}

{% block content %}
    {% load humanize mathfilters widget_tweaks %}

    <!-- Start Content-->
    <div class="container-fluid">

        <!-- start page title -->
        {% include 'rebs/partials/content_title.html' with  second_bs="신규 프로젝트" title="토지 정보 관리" %}
        <!-- end page title -->

        <div class="row">
            <form class="form-horizontal p-0 col-12" method="get">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">

                            {% with menu_order='3' %}
                                {% include 'rebs_project/partials/head_menu.html' %}
                            {% endwith %}

                            <div class="row">
                                {% include 'rebs/partials/project_select.html' %}
                                <div class="col-sm-12 col-md-6"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body pb-1">
                        <div class="row">
                            <div class="row">
                                <div class="col mb-3">
                                    <!-- Button Group -->
                                    <div class="btn-group mb-2">
                                        <button type="button" class="btn btn-primary">지번 별 토지관리</button>
                                        <button type="button" class="btn btn-light"
                                                onclick="location.href='{% url 'rebs:project:site-owner' %}'">소유자별 토지관리
                                        </button>
                                        <button type="button" class="btn btn-light"
                                                onclick="location.href='{% url 'rebs:project:site-contract' %}'">사업부지
                                            매입관리
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <form method="post" class="col-12 p-0 needs-validation" novalidate>
                                {% csrf_token %}
                                {{ form.project }}
                                {{ form.errors }}
                                <div class="col-12 form-row">
                                    <div class="col-sm-12 col-lg-1 bg-light text-center mb-1">
                                        <div class="col p-0 pt-2 pb-2 m-auto h-100"><strong>토지(지번) 등록</strong></div>
                                    </div>
                                    <div class="col-sm-12 col-lg-11">
                                        <div class="col-12 form-row mb-1">
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-1 mb-1">
                                                {{ form.order|add_class:"form-control"|attr:"placeholder:No." }}
                                                {{ form.order.errors }}
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-2 mb-1">
                                                {{ form.district|add_class:"form-control"|attr:"placeholder:행정동(Lot)" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-2 mb-1">
                                                {{ form.lot_number|add_class:"form-control"|attr:"placeholder:지번(산123-10)" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-1 mb-1">
                                                {{ form.site_purpose|add_class:"form-control"|attr:"placeholder:지목" }}
                                            </div>
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-2 mb-1">
                                                {{ form.official_area|add_class:"form-control"|attr:"placeholder:공부상면적(㎡)" }}
                                            </div>
                                            {% if this_project.is_returned_area %}
                                                <div class="col-xs-12 col-sm-6 col-md-3 col-lg-2 mb-1">
                                                    {{ form.returned_area|add_class:"form-control"|attr:"placeholder:환지면적(㎡)" }}
                                                </div>
                                            {% endif %}
                                            <div class="col-xs-12 col-sm-6 col-md-3 col-lg-2 mb-1">
                                                {{ form.dup_issue_date|add_class:"form-control"|attr:"placeholder:등본발급일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                            </div>
                                        </div>
                                        <div class="col-12 form-row">
                                            <div class="col-xs-12 col-lg-10 mb-1">
                                                {{ form.rights_restrictions|add_class:"form-control"|attr:"placeholder:권리제한사항"|attr:"rows:2" }}
                                            </div>
                                            {% if request.user.is_superuser or request.user.staffauth.project > '1' %}
                                                <div class="col-6 col-lg-1 text-center m-auto">
                                                    {% if request.GET.site %}<a href="{% url 'rebs:project:site' %}{% if request.GET.page %}?page={{ request.GET.page }}{% endif %}">Reset</a>{% endif %}
                                                </div>
                                            {% endif %}
                                            <div class="col-6 col-lg-{% if request.user.is_superuser or request.user.staffauth.project > '1' %}1{% else %}2{% endif %} text-right m-auto">
                                                {% if request.user.is_superuser or request.user.staffauth.project > '1' %}
                                                    <button type="submit" class="btn btn-{% if request.GET.site %}success{% else %}primary{% endif %}">
                                                        {% if request.GET.site %}변경{% else %}신규{% endif %}등록
                                                    </button>
                                                {% else %}
                                                    <button type="button" class="btn btn-outline-secondary"
                                                            onclick="alert('토지 정보 관리 권한이 없습니다. 관리자에게 문의하여 주십시요.')">조회권한 사용자
                                                    </button>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>

                            <div class="col-12 table-responsive">
                                <div class="row m-1 mt-3 mb-1">
                                    <div class="col-6 text-success">
                                        총 {{ total_site.count }}필지 {{ total_area|floatformat:"2"|default:"-"|intcomma }}m<sup>2</sup>
                                        ({{ total_area|mul:"0.3025"|floatformat:"2"|intcomma }}평) 등록
                                    </div>
                                    <div class="col-6 text-right">
                                        <a href="{% url 'excel:sites' %}?project={{ this_project.id }}">
                                            <i class="mdi mdi-file-excel-box"></i> Excel Export
                                            <i class="mdi mdi-download ml-1"></i>
                                        </a>
                                    </div>
                                </div>
                                <table id="scroll-horizontal-datatable"
                                       class="table table-hover table-bordered table-centered table-condensed table-sm w-100 m-0 nowrap">
                                    <thead>
                                    <tr class="bg-nav-pills">
                                        <th class="text-center" rowspan="2" style="width: 5%">No</th>
                                        <th class="text-center" rowspan="2" style="width: 7%">행정동</th>
                                        <th class="text-center" rowspan="2" style="width: 8%">지번</th>
                                        <th class="text-center" rowspan="2" style="width: 6%">지목</th>
                                        <th class="text-center" colspan="2">공부상 면적</th>
                                        {% if this_project.is_returned_area %}
                                            <th class="text-center" colspan="2">환지 면적</th>
                                        {% endif %}
                                        <th class="text-center" rowspan="2" style="width: 25%">소유자 목록</th>
                                        <th class="text-center" rowspan="2" style="width: 7%" scope="col">
                                            <a href="javascript: void(0);" class="action-icon">
                                                <i class="mdi mdi-pencil"></i></a>
                                            <a href="javascript: void(0);" class="action-icon">
                                                <i class="mdi mdi-delete"></i></a>
                                        </th>
                                    </tr>
                                    <tr class="bg-dragula">
                                        <th class="text-center" style="width: 9%">m<sup>2</sup></th>
                                        <th class="text-center" style="width: 8%">평</th>
                                        {% if this_project.is_returned_area %}
                                            <th class="text-center" style="width: 9%">m<sup>2</sup></th>
                                            <th class="text-center" style="width: 8%">평</th>
                                        {% endif %}

                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for object in object_list %}
                                        <tr>
                                            <td class="text-center">{{ object.order }}</td>
                                            <td class="text-center">{{ object.district }}</td>
                                            <td class="pl-3"><a href="{% url 'rebs:project:site' %}?{% if request.GET.page %}page={{ request.GET.page }}&{% endif %}site={{ object.id }}">{{ object.lot_number }}</a></td>
                                            <td class="text-center">{{ object.site_purpose }}</td>
                                            <td class="text-right">{{ object.official_area|floatformat:2|intcomma|default:"-" }}</td>
                                            <td class="text-right bg-warning-lighten">{{ object.official_area|mul:0.3025|floatformat:2|intcomma|default:"-" }}</td>
                                            {% if this_project.is_returned_area %}
                                                <td class="text-right">{{ object.returned_area|floatformat:2|intcomma|default:"-" }}</td>
                                                <td class="text-right bg-warning-lighten">{{ object.returned_area|mul:0.3025|floatformat:2|intcomma|default:"-" }}</td>
                                            {% endif %}
                                            <td class="text-center">
                                                {% for owner in object.siteowner_set.all %}{{ owner }}
                                                    {% if not forloop.last %}, {% endif %}
                                                {% endfor %}
                                            </td>
                                            <td class="text-center bg-dragula">
                                                <a href="{% url 'rebs:project:site' %}?{% if request.GET.page %}page={{ request.GET.page }}&{% endif %}site={{ object.id }}" class="action-icon">
                                                    <i class="mdi mdi-pencil"></i></a>
                                                <a href="javascript: site_del('{{ object }}', '{{ object.id }}');" class="action-icon">
                                                    <i class="mdi mdi-delete"></i></a>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        {% if is_paginated %}

                            {# to_first_last :: 맨처음 / 맨끝으로 버튼 사용 여부 #}
                            {# page_class ex:: pagination-rounded pagination-sm pagination-lg justify-content-center justify-content-end #}
                            {% include 'partials/pagination.html' with to_first_last="ok" page_class="pagination-rounded justify-content-end" %}

                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

    </div> <!-- container -->

{% endblock %}

{% block third_party_js %}
    <script>
      window.onload = function () {
        document.getElementById('id_project_sel').setAttribute('onchange', 'submit()')
      }

      function site_del(obj, id) {
        if (confirm('[' + obj + ']\n\n삭제 후 복구할 수 없습니다. 삭제하시겠습니까?')) {
          location.href = "/rebs/project/site-del/" + id
        } else {
          return
        }
      }
    </script>
{% endblock %}
